﻿using BaseDll;
using System;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using Sunny.UI;
using System.Collections.Generic;
using System.Reflection;
using Newtonsoft.Json;
namespace OtherDevice
{
    public class OtherDevicesData
    {
        #region 测高镭射
        [Category("测高镭射"), DisplayName("测高镭射"), Description("测高镭射,COM端口号")]
        public string LaserCom { set; get; }
        #endregion
               
        #region 电子天平
        [Category("电子天平"), DisplayName("电子天平"), Description("电子天平,COM端口号")]
        public string BalanceCom { set; get; }
        #endregion

        #region 光源控制器
        [Category("光源控制器"), DisplayName("光源控制器"), Description("光源控制器,COM端口号")]
        public string LightControlCom { set; get; }




        #endregion

        #region 视觉通信
        [Category("视觉通信"), DisplayName("视觉通信IP"), Description("视觉通信,Ip号")]
        public string Ip_视觉 { set; get; } = "";
        [Category("视觉通信"), DisplayName("视觉通信端口"), Description("视觉通信,Port")]
        public int Port_视觉 { set; get; } = 504;
        #endregion
    }

    public class DeviceInfo
    {
       public DeviceType deviceType = DeviceType.串口;
        public string deviceTypeName = "";
       public ComParamter Paramter_串口 = new ComParamter();
       public ClylinderParamter Paramter_气缸 = new ClylinderParamter();
        public TcpIpParamter Paramter_TcpIp = new TcpIpParamter();
        [JsonIgnore]
       public COM_Base Device_串口;
        [JsonIgnore]
        public ClylinerBase Device_气缸;
        [JsonIgnore]
        public TcpIp_Base Device_TcpIp;
    }


    public class DeviceMgr
    {
        private static object obj = new object();
        private static DeviceMgr deviceMgr;
        public Dictionary<string, DeviceInfo> dic_DeviceInfo = new Dictionary<string, DeviceInfo>();
        /// <summary>
        /// 获取外设数据管理句柄
        /// </summary>
        /// <returns></returns>
        public static DeviceMgr GetInstance()
        {
            if (deviceMgr == null)
            {
                lock (obj)
                {
                    deviceMgr = new DeviceMgr();
                    return deviceMgr;
                }

            }
            else
                return deviceMgr;
        }
        /// <summary>
        /// 读取外设数据
        /// </summary>
        /// <returns></returns>
        public int Read()
        {
            string currentFile = AppDomain.CurrentDomain.BaseDirectory + @"\config\DeviceData.json";
            if (!File.Exists(currentFile))
            {
                Save();
            }
            Object obs = AccessJosnSerializer.JsonToObject(currentFile, typeof(Dictionary<string, DeviceInfo>));
            if (obs != null)
            {
                dic_DeviceInfo = (Dictionary<string, DeviceInfo>)obs;
            }
            else
            {
                MessageBox.Show("外设数据加载失败，请确认");
            }
            return 0;
        }
        /// <summary>
        /// 保存外设数据
        /// </summary>
        /// <returns></returns>
        public int Save()
        {
            string currentNozzleFile = AppDomain.CurrentDomain.BaseDirectory + @"\config\DeviceData.json";
            bool a = AccessJosnSerializer.ObjectToJson(currentNozzleFile, dic_DeviceInfo);
            return 0;

        }

        public void Init()
        {

            foreach (var item in dic_DeviceInfo)
            {
               if (item.Value.deviceType == DeviceType.串口)
                {
                                 
                    if (item.Value.Device_串口.InitCom(item.Value.Paramter_串口)!=0)
                    {
                        MessageBox.Show($"{item.Key} 串口初始化失败，请检查");
                    }
                }
                else if (item.Value.deviceType == DeviceType.TcpIp)
                {
                    if (item.Value.Device_TcpIp.Init(item.Value.Paramter_TcpIp) != 0)
                    {
                        MessageBox.Show($"{item.Key} 网口初始化失败，请检查");
                    }
                }
                else if (item.Value.deviceType == DeviceType.气缸)
                {
                    if (item.Value.Device_气缸.Init(item.Value.Paramter_气缸) != 0)
                    {
                        MessageBox.Show($"{item.Key} 气缸初始化失败，请检查");
                    }
                }

            }
        }
        public COM_Base Function_获取串口(string strName)
        {
            if (dic_DeviceInfo.ContainsKey(strName))
            {
                return dic_DeviceInfo[strName].Device_串口;
            }
            else
                return null;
        }
        public TcpIp_Base Function_获取网口(string strName)
        {
            if (dic_DeviceInfo.ContainsKey(strName))
            {
                return dic_DeviceInfo[strName].Device_TcpIp;
            }
            else
                return null;

        }

        public ClylinerBase Function_获取气缸(string strName)
        {
            if (dic_DeviceInfo.ContainsKey(strName))
            {
                return dic_DeviceInfo[strName].Device_气缸;
            }
            else
                return null;

        }
        public void Function_动态创建设备()
        {
            foreach (var item in DeviceMgr.GetInstance().dic_DeviceInfo)
            {
                if (item.Value.deviceType == DeviceType.串口)
                {
                    if (item.Value.deviceTypeName == "")
                        continue;
                    Type Test = Type.GetType(item.Value.deviceTypeName);
                    object[] args = new object[]
                    {
                      item.Key
                    };
                    COM_Base com = Activator.CreateInstance(Test, args) as COM_Base;
                    if (com == null)
                        continue;
                    else
                        item.Value.Device_串口 = com;
                }
                else if (item.Value.deviceType == DeviceType.TcpIp)
                {
                    if (item.Value.deviceTypeName == "")
                        continue;
                    Type Test = Type.GetType(item.Value.deviceTypeName);
                    object[] args = new object[]
                    {
                      item.Key
                    };
                    TcpIp_Base tcpIp = Activator.CreateInstance(Test, args) as TcpIp_Base;

                    if (tcpIp == null)
                        continue;
                    else
                        item.Value.Device_TcpIp = tcpIp;

                }
                else if (item.Value.deviceType == DeviceType.气缸)
                {
                    ClylinerBase clylinerBase = new ClylinerBase();
                    item.Value.Device_气缸 = clylinerBase;
                    item.Value.Device_气缸.Init(item.Value.Paramter_气缸);
                }
            }


        }
    }
}